package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    /**
     * 获取时间段内的销售额信息
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatics(LocalDate beginTime, LocalDate endTime) {
        ArrayList<LocalDate> dateList = new ArrayList<>();

        dateList.add(beginTime);

        while (!beginTime.equals(endTime)){
            beginTime = beginTime.plusDays(1);
            dateList.add(beginTime);
        }

        ArrayList<Object> amoList = new ArrayList<>();
        //找出来了根据每天统计的营业总
        for (LocalDate date:dateList) {
            LocalDateTime begin = LocalDateTime.of(date, LocalDateTime.MIN.toLocalTime());
            LocalDateTime end = LocalDateTime.of(date, LocalDateTime.MAX.toLocalTime());

            BigDecimal amo = orderMapper.getSumByDay(begin,end, Orders.COMPLETED);
            amo = amo == null ? BigDecimal.valueOf(0) :amo;

            amoList.add(amo);
        }


        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .turnoverList(StringUtils.join(amoList, ","))
                .dateList(StringUtils.join(dateList, ","))
                .build();

        return turnoverReportVO;
    }


    /**
     * 统计用户报表
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public UserReportVO getUserStatics(LocalDate beginTime, LocalDate endTime) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        //时间
        dateList.add(beginTime);

        while (!beginTime.equals(endTime)){
            beginTime = beginTime.plusDays(1);
            dateList.add(beginTime);
        }

        //用户总数
        ArrayList<Integer> totalUserList = new ArrayList<>();
        ArrayList<Integer> increaseUserList = new ArrayList<>();
        for (LocalDate date:dateList) {
            LocalDateTime end = LocalDateTime.of(date, LocalTime.MAX);
            LocalDateTime begin = LocalDateTime.of(date, LocalTime.MIN);
            Integer totalUser = userMapper.sumTotalUserByTime(null, end);
            Integer increaseUser = userMapper.sumTotalUserByTime(begin, end);

            totalUserList.add(totalUser);
            increaseUserList.add(increaseUser);
        }

        UserReportVO userReportVO = UserReportVO.builder().totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(increaseUserList, ","))
                .dateList(StringUtils.join(dateList, ","))
                .build();

        return userReportVO;
    }



    /**
     * 统计指定时间区间内的订单数据
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //存放从begin到end之间的每天对应的日期
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //存放每天的订单总数
        List<Integer> orderCountList = new ArrayList<>();
        //存放每天的有效订单数
        List<Integer> validOrderCountList = new ArrayList<>();

        //遍历dateList集合，查询每天的有效订单数和订单总数
        for (LocalDate date : dateList) {
            //查询每天的订单总数 select count(id) from orders where order_time > ? and order_time < ?
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer orderCount = orderMapper.getOrderCount(beginTime, endTime, null);

            //查询每天的有效订单数 select count(id) from orders where order_time > ? and order_time < ? and status = 5
            Integer validOrderCount = orderMapper.getOrderCount(beginTime, endTime, Orders.COMPLETED);

            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }

        //计算时间区间内的订单总数量
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();

        //计算时间区间内的有效订单数量
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            //计算订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }

        return  OrderReportVO.builder()
                .dateList(org.apache.commons.lang3.StringUtils.join(dateList,","))
                .orderCountList(org.apache.commons.lang3.StringUtils.join(orderCountList,","))
                .validOrderCountList(org.apache.commons.lang3.StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }


    /**
     * 销量前十商品统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);

        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);


        List<GoodsSalesDTO> goodsSalesDTOS = orderDetailMapper.getSaleTop10(beginTime,endTime);

        List<String> goodsNameList = goodsSalesDTOS.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> goodsNumberList= goodsSalesDTOS.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());

        SalesTop10ReportVO salesTop10ReportVOBuilder = SalesTop10ReportVO.builder().nameList(StringUtils.join(goodsNameList, ","))
                .numberList(StringUtils.join(goodsNumberList, ",")).build();

        return salesTop10ReportVOBuilder;
    }

}
